﻿namespace Pps.Gui.Base
{
    using Microsoft.SPOT;
    using Media = Microsoft.SPOT.Presentation.Media;

    public class Graphics
    {
        public const int BLACKNESS = 4;
        public const int DSTDKGRAY = 8;
        public const int DSTGRAY = 6;
        public const int DSTINVERT = 3;
        public const int DSTLTGRAY = 7;
        public const uint DT_AlignmentCenter = 2;
        public const uint DT_AlignmentLeft = 0;
        public const uint DT_AlignmentMask = 34;
        public const uint DT_AlignmentRight = 32;
        public const uint DT_IgnoreHeight = 16;
        public const uint DT_None = 0;
        public const uint DT_TrimmingCharacterEllipsis = 64;
        public const uint DT_TrimmingMask = 72;
        public const uint DT_TrimmingNone = 0;
        public const uint DT_TrimmingWordEllipsis = 8;
        public const uint DT_TruncateAtBottom = 4;
        public const uint DT_WordWrap = 1;
        public const ushort OpacityOpaque = 256;
        public const ushort OpacityTransparent = 0;
        public const int PATINVERT = 2;
        public const int RANDOM = 10;
        public const int SINGLEPIXEL = 9;
        public const int SRCCOPY = 1;
        public const int WHITENESS = 5;

        private static Bitmap gfx = new Bitmap(Bitmap.MaxWidth, Bitmap.MaxHeight);

        private int offsetX;
        private int offsetY;

        public Graphics(int offsetX, int offsetY)
        {
            this.offsetX = offsetX;
            this.offsetY = offsetY;
        }

        public Graphics(Graphics graphics, int offsetX, int offsetY)
        {
            this.offsetX = graphics.offsetX + offsetX;
            this.offsetY = graphics.offsetY + offsetY;
        }

        public void DrawEllipse(Color colorOutline, int x, int y, int xRadius, int yRadius)
        {
            gfx.DrawEllipse((Media.Color)colorOutline, x + this.offsetX, y + this.offsetY, xRadius, yRadius);
        }

        public void DrawEllipse(Color colorOutline, int thicknessOutline, int x, int y, int xRadius, int yRadius, Color colorGradientStart, int xGradientStart, int yGradientStart, Color colorGradientEnd, int xGradientEnd, int yGradientEnd, ushort opacity)
        {
            gfx.DrawEllipse((Media.Color)colorOutline, thicknessOutline, x + this.offsetX, y + this.offsetY, xRadius, yRadius, (Media.Color)colorGradientStart, xGradientStart, yGradientStart, (Media.Color)colorGradientEnd, xGradientEnd, yGradientEnd, opacity);
        }

        public void DrawImage(int xDst, int yDst, Bitmap bitmap, int xSrc, int ySrc, int width, int height)
        {
            gfx.DrawImage(xDst + this.offsetX, yDst + this.offsetY, bitmap, xSrc, ySrc, width, height);
        }

        public void DrawImage(int xDst, int yDst, Bitmap bitmap, int xSrc, int ySrc, int width, int height, ushort opacity)
        {
            gfx.DrawImage(xDst + this.offsetX, yDst + this.offsetY, bitmap, xSrc, ySrc, width, height, opacity);
        }

        public void DrawLine(Color color, int thickness, int x0, int y0, int x1, int y1)
        {
            gfx.DrawLine((Media.Color)color, thickness, x0 + this.offsetX, y0 + this.offsetY, x1 + this.offsetX, y1 + this.offsetY);
        }

        public void DrawRectangle(Color colorOutline, int thicknessOutline, int x, int y, int width, int height, int xCornerRadius, int yCornerRadius, Color colorGradientStart, int xGradientStart, int yGradientStart, Color colorGradientEnd, int xGradientEnd, int yGradientEnd, ushort opacity)
        {
            gfx.DrawRectangle((Media.Color)colorOutline, thicknessOutline, x + this.offsetX, y + this.offsetY, width, height, xCornerRadius, yCornerRadius, (Media.Color)colorGradientStart, xGradientStart, yGradientStart, (Media.Color)colorGradientEnd, xGradientEnd, yGradientEnd, opacity);
        }

        public void DrawText(string text, Font font, Color color, int x, int y)
        {
            gfx.DrawText(text, font, (Media.Color)color, x + this.offsetX, y + this.offsetY);
        }

        public void DrawTextInRect(string text, int x, int y, int width, int height, uint dtFlags, Color color, Font font)
        {
            gfx.DrawTextInRect(text, x + this.offsetX, y + this.offsetY, width, height, dtFlags, (Media.Color)color, font);
        }

        public bool DrawTextInRect(ref string text, ref int xRelStart, ref int yRelStart, int x, int y, int width, int height, uint dtFlags, Color color, Font font)
        {
            return gfx.DrawTextInRect(ref text, ref xRelStart, ref yRelStart, x + this.offsetX, y + this.offsetY, width, height, dtFlags, (Media.Color)color, font);
        }

        public void Flush()
        {
            gfx.Flush();
        }

        public static void Flush(int x, int y, int width, int height)
        {
            gfx.Flush(x, y, width, height);
        }
    }
}